home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7811 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: tech.cftnet.com!not-for-mail
  2. From: wcowley@cftnet.com (Wes Cowley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: problem: static data in classes
  5. Date: 19 Feb 1996 10:35:08 GMT
  6. Organization: CFTnet
  7. Message-ID: <4g9jot$aur@tech.cftnet.com>
  8. References: <9602180123.AA22010@dcc11748.slip.digex.net>
  9. NNTP-Posting-Host: ppp244_4.cftnet.com
  10. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  11.  
  12. Steven D. Arnold (yami@digex.net) wrote:
  13. : [...]
  14. : class sample
  15. : {
  16. :         public:
  17. :                 sample();
  18. :         private:
  19. :                 static int count;
  20. : };
  21. : [...]
  22. : Under MetroWerks, I get the error:
  23. :         Link Error   : newtest.cpp: 'sample::count' referenced from
  24. :                 'sample::sample()' is undefined.
  25. : Under g++, I see:
  26. :         ld: Undefined symbol
  27. :         sample::count
  28.  
  29.  
  30. You've declared sample::count but nowhere did you provide a definition 
  31. for it.  The definition is where space is actually allocated for the 
  32. variable and is required as a static member variable doesn't get 
  33. allocated with each instance.
  34.  
  35. What the compilers are complaining about is a missing line like:
  36.  
  37. int sample::count = 0;
  38.  
  39. somewhere outside the class declaration.
  40.  
  41. Wes
  42.